<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Return a well-formed XML string based on SimpleXML element</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="simplexmlelement.addchild.html">SimpleXMLElement::addChild</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="simplexmlelement.attributes.html">SimpleXMLElement::attributes</a></div>
 <div class="up"><a href="class.simplexmlelement.html">SimpleXMLElement</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="simplexmlelement.asxml" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">SimpleXMLElement::asXML</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.0.1)</p><p class="refpurpose"><span class="refname">SimpleXMLElement::asXML</span> &mdash; <span class="dc-title">
   Return a well-formed XML string based on SimpleXML element
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-simplexmlelement.asxml-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>SimpleXMLElement::asXML</strong></span>
    ([ <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span>
  ] )</div>

  <p class="para rdfs-comment">
   The <em>asXML</em> method formats the parent object&#039;s data
   in XML version 1.0.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-simplexmlelement.asxml-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">filename</code></em></dt>

     <dd>

      <p class="para">
       If specified, the function writes the data to the file rather than
       returning it.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-simplexmlelement.asxml-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   If the <em><code class="parameter">filename</code></em> isn&#039;t specified, this function
   returns a <span class="type"><a href="language.types.string.html" class="type string">string</a></span> on success and <strong><code>FALSE</code></strong> on error. If the
   parameter is specified, it returns <strong><code>TRUE</code></strong> if the file was written
   successfully and <strong><code>FALSE</code></strong> otherwise.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-simplexmlelement.asxml-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5379">
    <p><strong>Example #1 Get XML</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$string&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;XML<br /></span><span style="color: #DD0000">&lt;a&gt;<br />&nbsp;&lt;b&gt;<br />&nbsp;&nbsp;&lt;c&gt;text&lt;/c&gt;<br />&nbsp;&nbsp;&lt;c&gt;stuff&lt;/c&gt;<br />&nbsp;&lt;/b&gt;<br />&nbsp;&lt;d&gt;<br />&nbsp;&nbsp;&lt;c&gt;code&lt;/c&gt;<br />&nbsp;&lt;/d&gt;<br />&lt;/a&gt;<br /></span><span style="color: #007700">XML;<br /><br /></span><span style="color: #0000BB">$xml&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">asXML</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;a&gt;
 &lt;b&gt;
  &lt;c&gt;text&lt;/c&gt;
  &lt;c&gt;stuff&lt;/c&gt;
 &lt;/b&gt;
 &lt;d&gt;
  &lt;c&gt;code&lt;/c&gt;
 &lt;/d&gt;
&lt;/a&gt;
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <em>asXML</em> also works on Xpath results:
   <div class="example" id="example-5380">
    <p><strong>Example #2 Using asXML() on <span class="methodname"><a href="simplexmlelement.xpath.html" class="methodname">SimpleXMLElement::xpath()</a></span> results</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Continued&nbsp;from&nbsp;example&nbsp;XML&nbsp;above.<br /><br />/*&nbsp;Search&nbsp;for&nbsp;&lt;a&gt;&lt;b&gt;&lt;c&gt;&nbsp;*/<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">xpath</span><span style="color: #007700">(</span><span style="color: #DD0000">'/a/b/c'</span><span style="color: #007700">);<br /><br />while(list(&nbsp;,&nbsp;</span><span style="color: #0000BB">$node</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">each</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$node</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">asXML</span><span style="color: #007700">();<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
&lt;c&gt;text&lt;/c&gt;&lt;c&gt;stuff&lt;/c&gt;
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-simplexmlelement.asxml-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="simplexmlelement.tostring.html" class="methodname" rel="rdfs-seeAlso">SimpleXMLElement::__toString()</a> - Returns the string content</span></li>
    <li class="member"><a href="simplexml.examples-basic.html" class="xref">Basic SimpleXML usage</a></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="simplexmlelement.addchild.html">SimpleXMLElement::addChild</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="simplexmlelement.attributes.html">SimpleXMLElement::attributes</a></div>
 <div class="up"><a href="class.simplexmlelement.html">SimpleXMLElement</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
